import 'package:http/http.dart';
import 'package:time/http/http_config.dart';

class HttpLog {
  static void log(Response response) {
    if (HttpConfig.logEnable) {
      logRequest(response);
      logResponse(response.body);
    }
  }

  static void logRequest(Response response) {
    print("▶ methord: " + response.request!.method);
    print("▶ url: " + response.request!.url.toString());
  }

  static void logResponse(jsonObject) {
    //打印返回json数据
    print(
        "▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼");

    var content = jsonObject.toString();
    var maxLen = 120;
    var lines = content.length / maxLen;

    if (content.length % maxLen != 0) lines++;

    for (int i = 0; i < lines; i++) {
      var startIndex = i * maxLen;
      if (startIndex > content.length) break;
      var endIndex = (i + 1) * maxLen;
      if (endIndex > content.length) {
        endIndex = content.length;
      }
      print("◆ " + content.substring(startIndex, endIndex));
    }

    print(
        "▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲");
  }
}
